package com.icss.th.question.service;

import java.util.*;
import java.io.*;

public class SimilarQuestionService
{
	/**
	*	@todo move this as a string to "system.properties"
	*/
	static final String SIMILAR_QUESTION_HOME = "D:/XUE/Tihai/data/output/examcn"; 
	//"/home/colin/software/th/similar";
	
	public static int[] getNeighbors(int examID, int typeID) throws Exception
	{
		/**
		*	@return an int array, length = 50; or null if similar questions data not found
		*/
		
		// find baseFolder first
		StringBuilder sbFilename = new StringBuilder().append(SIMILAR_QUESTION_HOME).append("/").append(typeID);
		String fname = sbFilename.toString();
		// read question offset hashtable
		ObjectInputStream binFile = new ObjectInputStream(new FileInputStream(fname + ".hashtable"));
		Hashtable<Integer, Integer> typeOffsets = (Hashtable<Integer, Integer>)binFile.readObject();
		int offset = typeOffsets.get(new Integer(examID)).intValue();
		// read 50 int (200 bytes) from data file
		RandomAccessFile dbfile = new RandomAccessFile(fname + ".arrays", "r");
		dbfile.seek(offset);
		int[] neighbors = new int[50];
		for (int i = 0; i < 50; i++)
			neighbors[i] = dbfile.readInt();
			
		return neighbors;
	}
}